<template>
	<view class="content">
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" :placeholder='true' @leftClick="leftClick('/pages/mine/index')"
				title="我的商品">
			</u-navbar>
		</view>
		<sh-filter :brandlist="brandlist" :catelist="catelist" @change="onFilter"></sh-filter>
		<good-card :hasadd="false" :goodlist="goodslist"></good-card>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		getMinegoods,
		getMinegoodscate,
		getMinegoodsbrand
	} from '@/network/api/user.js'
	import goodCard from '../../../components/goodCard.vue';
	import shFilter from '../../classify/search/components/sh-filter.vue'
	export default {
		data() {
			return {
				brandlist: [],
				catelist: [],
				goodslist: [],
				page: 1,
				data: {
					company_id: '',
					id: '',
					successively: 'asc',
					brand: '',
					sort: '',
					page: 1,
					search: ''
				},
				successively: ['desc', 'asc']
			}
		},
		components: {
			shFilter,
			goodCard
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
			})
		},
		onShow() {
      this.page=1
			this.data.page=1
			this.goodslist=[]
			this.getcate()
			this.getbrand()
			this.getmygood()
		},
		onReachBottom() {
			this.page++
			this.getmygood()
		},
		methods: {
			onFilter(e) {
				console.log(e);
				console.log(e);
				if (e.defaultOrder) {
					this.data.successively = this.successively[e.defaultOrder]
					this.data.sort = 'b.createtime'
				}
				if (!e.defaultOrder) {
					this.data.successively = this.successively[e.defaultOrder]
					this.data.sort = ''
				}
				if (e.salesOrder) {
					this.data.successively = this.successively[e.salesOrder - 1]
					this.data.sort = 'b.virtual_sales'
				}
				if (e.priceOrder) {
					this.data.successively = this.successively[e.priceOrder - 1]
					this.data.sort = 'b.shop_price'
				}
				this.data.brand = e.brand_id
				this.data.id = e.cate_id || ''
				this.page = 1
				this.goodslist = []
				this.getmygood()

			},
			async getcate() {
				let data = {
					id: '',
					brand_id: ''
				}
				const res = await getMinegoodscate(data)
				this.catelist = res.data.categorylist

			},
			async getbrand() {
				let data = {
					id: '',
					brand_id: ''
				}
				const res = await getMinegoodsbrand(data)
				this.brandlist = res.data.brand
			},
			async getmygood() {
				// this.data = {
				// 	company_id: this.userinfo.company_id,
				// 	id: '',
				// 	successively: 'asc',
				// 	brand: '',
				// 	sort: '',
				// 	page: this.page,
				// 	search: ''
				// }

				this.data.company_id = this.userinfo.company_id
				this.data.page = this.page
				uni.showLoading({
					title: "加载中，请稍后...",
					mask: true,
				});
				const res = await getMinegoods(this.data)
				uni.hideLoading()
				let newgoodslist = res.data.personal.data
				this.goodslist = [...this.goodslist, ...newgoodslist]
        if(this.goodslist.length==0){
          uni.showToast({
            title: '暫無數據',
            icon: 'none',
            duration: 2000
          })
        }
			},

			leftClick(url) {
				uni.switchTab({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss">
</style>
